﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Windows.Data.Xml.Dom;
using Windows.UI.Popups;
using System.Linq;
using Windows.Storage;
using System.Threading.Tasks;
using SharPy.GameSaves;
using SharPy.GameObjects;

namespace SharPy.WebObjects
{
    internal class clXmlHandler
    {
        string xmlString;

        internal clXmlHandler()
        {
        }

        internal async Task<string> getXmlFromFile(StorageFile pFile)
        {
            string toReturn = await FileIO.ReadTextAsync(pFile);
            return toReturn;
        }

        internal async Task<List<clGameInfo>> getGameInfo(string pData)
        {
            List<clGameInfo> toReturn = new List<clGameInfo>();
            string[] lines = pData.Split(Environment.NewLine.ToCharArray()).Skip(1).ToArray();
            string output = string.Join(Environment.NewLine, lines);
            StringReader rdata = new StringReader(output.Replace(" ", "").Replace("\r", ""));
            XmlReader reader = XmlReader.Create(rdata);
            XElement root = XElement.Load(reader);
            IEnumerable<XElement> gameElement =
                from el in root.Elements("game")
                select el;
            foreach (XElement el in gameElement)
            {
                toReturn.Add(
                    new clGameInfo(el.Elements("name").First().Value,
                                   el.Elements("url").First().Value,
                                   "Test game"
                    )
                );
            }
            return toReturn;
        }

    }
}